if minetest.setting_getbool("portal_eatdust")~= false then
	minetest.setting_set("portal_eatdust", "true")
end

minetest.register_node("lunocarpets:portal_exclusive", {
	description = "Carpete Portal Exclusivo",
	drawtype = "nodebox",
	paramtype = "light", --Não sei pq, mas o bloco não aceita a luz se nao tiver esta propriedade
	paramtype2 = "facedir",
	--sunlight_propagates = true,
	--light_source = LIGHT_MAX, --Este tapete brilha no escuro pq é mágico.
	node_box = {
		type = "fixed",
		fixed = { {-.5,-.5,-.5,    .5,-.47,.5}	}
	},
	selection_box = {
		type = "fixed",
		fixed = { {-.5,-.5,-.5,    .5,-.47,.5}	}
	},
	tiles = {
		"teleport_exclusive_96x96.png", --acima
		"teleport_exclusive_96x96.png^[transformfx",  --abaixo
		--"tapete_lado.png",  --abaixo
		"tapete_lado.png" --lados
	},
	groups = {vessel=1,dig_immediate=3,attached_node=1},
	--sounds = default.node_sound_wood_defaults(),
	after_place_node = function(pos, placer, itemstack)
		local owner = placer:get_player_name()
		local valor = lunomods.getCharValue(owner, "posPortal")
		if valor~=nil and valor~="" then
			posOld = minetest.string_to_pos(valor)
			local meta1 = minetest.env:get_meta(posOld)
			meta1:set_string("infotext", "Carpete Portal Exclusivo de '"..owner.."'.")
			meta1:set_string("owner",owner)
			meta1:set_string("toPortal", minetest.pos_to_string(pos))
			
			local meta2 = minetest.env:get_meta(pos)
			meta2:set_string("infotext", "Carpete Portal Exclusivo de '"..owner.."'.")
			meta2:set_string("owner",owner)
			meta2:set_string("toPortal", minetest.pos_to_string(posOld))
			
			lunomods.setCharValue(owner, "posPortal", nil)
		else
			local meta = minetest.env:get_meta(pos)
			meta:set_string("infotext", "Carpete Portal Exclusivo de '"..owner.."'. (Desativo)")
			meta:set_string("owner",owner)
			lunomods.setCharValue(owner, "posPortal", minetest.pos_to_string(pos))
		end
	end,
	can_dig = function(pos, player)
		local myDiger = player:get_player_name()
		local myOwner = minetest.env:get_meta(pos):get_string("owner")
		
		if myDiger~=nil and myOwner~=nil and myDiger == myOwner then
			local toPortal = minetest.env:get_meta(pos):get_string("toPortal")

			if toPortal~=nil and toPortal~="" then
				local meta = minetest.env:get_meta(minetest.string_to_pos(toPortal))
				meta:set_string("infotext", "Carpete Portal Exclusivo de '"..myDiger.."'. (Desativo)")
				meta:set_string("toPortal", "")
				lunomods.setCharValue(myDiger, "posPortal", toPortal)
			else
				lunomods.setCharValue(myDiger, "posPortal", nil)
			end
		else
			minetest.sound_play("sfx_falha", {pos=pos, max_hear_distance = 10})
		end
		
		return myOwner==nil or myOwner=="" or myDiger == myOwner
	end,
	on_rightclick = function(pos, node, clicker, itemstack)
		local myClicker = clicker:get_player_name()
		local myOwner = minetest.env:get_meta(pos):get_string("owner")
		local toPortal = minetest.env:get_meta(pos):get_string("toPortal")
		if toPortal~=nil and toPortal~="" then
			if myOwner~=nil and myOwner~="" and myOwner==myClicker then
				local item = clicker:get_wielded_item()
				local seComePo=minetest.setting_getbool("portal_eatdust")
				if seComePo~=true or (seComePo==true and item:get_name() == "lunorecipes:magicdust") then
					if seComePo==true and not minetest.setting_getbool("creative_mode") then 
						itemstack:take_item() 
					end

					local toPos = minetest.string_to_pos(toPortal)
					--minetest.sound_play("sfx_teleporte", {pos=pos, max_hear_distance = 10}) --toca som "sfx_teleporte".ogg a distancia de 10 blocos do usuario.
					clicker:setpos(toPos)
					--minetest.sound_play("sfx_teleporte", {pos=toPos, max_hear_distance = 10}) --toca som "sfx_teleporte".ogg a distancia de 10 blocos do usuario.
				
					return itemstack
				else
					minetest.chat_send_player(name, "Voce precisa utilizar um 'Po Encantado' neste Carpete Portal Exclusivo.")
					--minetest.sound_play("sfx_falha", {pos=pos, max_hear_distance = 10}) --toca som "sfx_teleporte".ogg a distancia de 10 blocos do usuario.
				end
			else
				minetest.chat_send_player(myClicker, "Este Carpete Portal Exclusivo so pode ser usado por '"..myOwner.."'!")
				--OBS.: Nao e bom por mensagem para nao poluir a caixa de dialogo.
				--minetest.sound_play("sfx_falha", {pos=pos, max_hear_distance = 10})
			end
		else
			minetest.chat_send_player(myClicker, "Este Carpete Portal Exclusivo ainda nao esta ativo.")
			minetest.sound_play("sfx_falha", {pos=pos, max_hear_distance = 10})
			-- Como o portal esta desativado ele nao emite nenhum som.
		end
	end,
})

minetest.register_craft({
	output = 'lunocarpets:portal_exclusive 2',
	recipe = {
		{"lunorecipes:magicdust", 	"", 							"lunorecipes:magicdust"},
		{"lunocarpets:wellcome", 	"default:steel_ingot",	"lunocarpets:wellcome"},
		{"default:mese", 				"",							"default:mese"}
	}
})

minetest.register_craft({
	output = 'lunocarpets:portal_exclusive',
	recipe = {
		{"default:steel_ingot"},
		{"lunocarpets:portal"}
	}
})


minetest.register_alias("portalcarpetexclusive"		,"lunocarpets:portal_exclusive")
minetest.register_alias("portalcarpetlocked"			,"lunocarpets:portal_exclusive")
minetest.register_alias("carpeteportalexclusivo"	,"lunocarpets:portal_exclusive")
minetest.register_alias("carpeteportaltrancado"		,"lunocarpets:portal_exclusive")
minetest.register_alias("portalexclusivo"				,"lunocarpets:portal_exclusive")
minetest.register_alias("portalexclusive"				,"lunocarpets:portal_exclusive")
minetest.register_alias("portaltrancado"				,"lunocarpets:portal_exclusive")
minetest.register_alias("portallocked"					,"lunocarpets:portal_exclusive")

